import java.util.ArrayList;
import java.util.Objects;
import java.util.Random;
import java.util.Scanner;

public class First {
    public static void main(String[] args){
        ArrayList<User> list=new ArrayList<>();
       User leader=new User("钞人","001","000","000000000000000000");
       list.add(leader);
        Scanner scanner=new Scanner(System.in);
        System.out.println(".................欢迎来到学生管理系统....................");
        while(true){
            System.out.println("1,登录  2，注册  3，忘记密码  4:退出");
            String input=scanner.next();
            if(input.equals("4")){
                break;
            }
            switch (input) {
                case "1" -> Login(list);
                case "2" -> Sign(list);
                case "3" -> Forget(list);
                case "9"->System.out.println(list);
                default -> System.out.println("输入异常,请重新输入");
            }
        }
    }
    private static void Login(ArrayList<User> list) {
        Scanner scanner=new Scanner(System.in);
        String userid;
        String password;
        while(true){
            System.out.println("输入用户ID");
            userid=scanner.next();
            System.out.println("输入密码");
            password=scanner.next();
            while(true){
                String code=code();
                System.out.println("验证码为"+code);
                System.out.println("输入验证码");
                String codeInput=scanner.next();
                if(codeInput.equals(code)){
                    break;
                }else{
                    System.out.println("验证码输入错误，请重新输入");
                }
            }
            User userlogin=new User(null,userid,password,null);
            boolean idpassword=checklogin(list,userlogin);
            if(idpassword){
                System.out.println("登录成功，正为您跳转页面");
                break;
            }else{
                System.out.println("用户ID或密码输入错误，请重新输入");
            }
        }
    }
    private static void Sign(ArrayList<User> list) {
        Scanner scanner = new Scanner(System.in);
        //用户名
        String username;
        while (true) {
            System.out.println("输入用户名");
            username = scanner.next();
            boolean flag1 = nameonlyone(list, username);
            if (!flag1) {
                System.out.println("该用户名已被使用，请换一个");
            } else {
                break;
            }
        }
        //用户id

//将数字转换成字符串
        String userid=Integer.toString(1000+list.size());
        System.out.println("你的ID为" + userid);
        /*while (true) {
            boolean flag2 = idonlyone(list);
            if (flag2) {
                System.out.println("你的ID为" + userid);
                break;
            }
        }*/

        //密码
        String password;
        while (true) {
            System.out.println("请输入密码");
            password = scanner.next();
            System.out.println("请再次输入确定密码");
            String againpassword = scanner.next();
            if (!password.equals(againpassword)) {
                System.out.println("请确认两次密码是否输入一致");
            } else {
                break;
            }
        }
        //身份证
        String idcard;
        while (true) {
            System.out.println("请输入身份证");
            idcard = scanner.next();
            if (idcard.length() == 18) {
                break;
            } else {
                System.out.println("请输入规定的身份证号码");
            }
        }
        //把数据封装成一个对象，传输参数时只需传输这个整体
        User usersign = new User(username, userid, password, idcard);
        list.add(usersign);
        System.out.println("注册成功，请前往登录吧");
    }
    private static void Forget(ArrayList<User> list) {
        while(true) {
            Scanner scanner=new Scanner(System.in);
            System.out.println("输入用户名");
            String userId = scanner.next();
            System.out.println("输入身份证号码");
            String idcard = scanner.next();
            User userforget=new User(null,userId,null,idcard);
            boolean forgetpassword=checkforget(list,userforget);

            if(forgetpassword){
                System.out.println("密码修改成功，请先往登录");
                break;
            } else{
                System.out.println("ID或身份证号码输入错误，请重新输入");
            }
        }
    }
    //验证用户名的唯一性
    private static boolean nameonlyone(ArrayList<User> list,String username){
        for(int i=0;i<list.size();i++){
            //从集合中获取到用户名字
            User user=list.get(i);
            String rightUsername=user.getUsername();
            if(username.equals(rightUsername)){
                return false;
            }
        }
        return true;
    }
    // //家人们，我被抛弃了
    // //验证ID的唯一性
    // private static  boolean idonlyone(ArrayList<User> list){
    //
    //     Random random=new Random();
    //     int id = random.nextInt(10000, 100000);
    //     for(int i=0;i<list.size();i++){
    //         User user=list.get(i);
    //         //将字符串转换成数字
    //         int rightid=Integer.parseInt(user.getUserid());
    //         if(id ==rightid){
    //             return false;
    //         }
    //    }
    //     return true;
    // }
    //难点->下面
    public static boolean checkforget(ArrayList<User> list,User userforget){
        User user=null;
        for(int i=0;i<list.size();i++){
            User temp=list.get(i);
            if(temp.getUserid().equals(userforget.getUserid())&& temp.getIdcard().equals(userforget.getIdcard())) {
                //将temp赋值给空的对象user（索引为i）
                user = temp;
            }
        }
        if(user == null){
            return false;
        }
        while(true){
            System.out.println("请输入新的密码");
            Scanner scanner=new Scanner(System.in);
            String newpassword=scanner.next();
            System.out.println("请再次输入确定密码");
            String againnewpassword=scanner.next();
            if(againnewpassword.equals(newpassword)){
                //修改掉索引为i的user的password
                user.setPassword(newpassword);
                return true;
            }else{
                System.out.println("两次密码输入不一致，请重新输入");
            }

        }
    }
    public static boolean checklogin(ArrayList<User> list,User userlogin){
        for(int i=0;i<list.size();i++){
            User user=list.get(i);
            if(Objects.equals(user.getUserid(), userlogin.getUserid()) && Objects.equals(user.getPassword(), userlogin.getPassword())){
                return true;
            }
        }
        return false;
    }
    public static String code(){
            ArrayList<Character> codelist=new ArrayList<>();
            for(int i=0;i<26;i++){
                codelist.add((char)('a'+i));
                codelist.add((char)('A'+i));
            }
            StringBuilder sb=new StringBuilder();
            Random random=new Random();
            for(int i=0;i<4;i++){
                int index=random.nextInt(codelist.size());
                char c=codelist.get(index);
                sb.append(c);
            }
            int num=random.nextInt(10);
            sb.append(num);
                char[] arr=sb.toString().toCharArray();
                int randomindex=random.nextInt(arr.length-1);
                //a.b||a->c,b->a,c->b
                char temp=arr[randomindex];
                arr[randomindex]=arr[arr.length-1];
                arr[arr.length-1]=temp;
                return new String(arr);
    }
}
